<?php
/**
 *
 * This is an example of a method to save
 * database data.
 *
 * If the "cntr_id" parameter is -1, it's a new record.
 * Otherwise, we're updating an old record.

 */
 if (!isset($gCms)) exit;


/**
 * After this, the code is identical to the code that would otherwise be
 * wrapped in the action.
 */

// get our records from the database
$db = $gCms->GetDb();


if (isset($params['cntr_id']) && $params['cntr_id'] != -1)
{
	// we received a cntr_id that was not -1, which means we're updating an
	// existing record. So we issue a SQL Update
   $query = 'UPDATE '.cms_db_prefix().
      'module_counters set name=?, description=?, explanation=?, value=?, ceiling=?, product_id=? where cntr_id = ?';
   $result = $db->Execute($query, array(    $params['name'],
                                            $params['description'],
                                            $params['explanation'],
                                            $params['value'],
                                            $params['ceiling'],
                                            $params['product_id'],
                                            $params['cntr_id']));
   $params['module_message'] = $this->Lang('updated_record');
}
else
{
    # why should it be set I don't know however here goes (JM)
    if (!isset($params['value'])) {$params['value'] = 0;}
    
   $query = 'INSERT INTO '.cms_db_prefix().
      'module_counters (name, description, explanation, value, ceiling, product_id) VALUES (?,?,?,?,?,?)';
   $result = $db->Execute($query, array(    $params['name'],
                                            $params['explanation'],
                                            $params['description'],
                                            $params['value'],
                                            $params['ceiling'],
                                            $params['product_id']));
   $params['module_message'] = $this->Lang('added_record');
}

if ($result === false)
{
   // yeah, that's graceful :(
   echo "Database error!";
   exit;
}

unset($params['cntr_id']);
// set a message and return to the page.
#$this->Redirect($id, 'default', $returnid, $params);
$this->Redirect($id, "defaultadmin", '', array('msg'=>'message_success', 'tab'=>'counters'));
$this->Redirect($id, 'defaultadmin', $returnid, $parms);
?>